/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package utils;

import java.io.PrintStream;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author e237573
 */
public class Logger {
    private static Logger instance = null;
    private PrintStream output;
    
    public enum Severity { INFO, WARNING, ERROR };
    
    private Map<Severity, Boolean> logged;
    
    private Logger() {
        this.logged = new HashMap<>();
        this.logged.put(Severity.INFO, false);
        this.logged.put(Severity.WARNING, true);
        this.logged.put(Severity.ERROR, true);
        this.output = System.out;
    }
    
    public void setOutput(PrintStream output)
    {
    	this.output = output;    	
    }
    
    public void setLog(Severity s, boolean log) {
        this.logged.put(s, log);
    }
    
    public static Logger getInstance() {
        if (instance == null)
            instance = new Logger();
        return instance;
    }
    
    public static void log(Object s, Severity severity) {
        getInstance().log(s, severity, 3);
    }
    
    public static void info(Object s)
    {
        getInstance().log(s, Severity.INFO, 3);
    }
    
    public static void warning(Object s)
    {
        getInstance().log(s, Severity.WARNING, 3);
    }
    
    public static void error(Object s)
    {
        getInstance().log(s, Severity.ERROR, 3);
    }    
    
    private void log(Object s, Severity severity, int depth) {
        if (this.logged.get(severity)) {
            StackTraceElement lastCalledFunction = Thread.currentThread().getStackTrace()[depth];
            String str = severity + " at " + lastCalledFunction.getClassName()
                    + "." + lastCalledFunction.getMethodName()
                    + ":" + lastCalledFunction.getLineNumber() 
                    + " - " + s;
        switch( severity ) {
            case INFO:
                output.println(str);
                output.flush();
                break;
            case WARNING:
                output.println("\u001B[33m" + str + "\u001B[0m");
                output.flush();
                break;
            case ERROR:
            	output.println("\u001B[31m" + str + "\u001B[0m");
            	output.flush();                
                System.exit(1);
                
        }
        }
    }
}
